import useSettingsStore from "@/store/modules/settings";
import tinycolor from "tinycolor2";

const settingsStore = useSettingsStore()
// 主题颜色
const theme = computed(() => settingsStore.theme);
const ANT_TAG_COLOR = ['processing', 'success', 'error', 'warning', 'magenta', 'red',
  'volcano', 'orange', 'gold', 'lime', 'green', 'cyan', 'blue', 'geekblue', 'purple']
export const getAntTagColor=(index:number)=>{
  return ANT_TAG_COLOR[index]
}

const hexToHSL = (hex: string) => {
  const color = tinycolor(theme);
  return color.toHsl();
}

const HSLToHex = (h: number, s: number, l: number) => {
  return tinycolor({ h, s, l }).toHexString();
}

/* 生成标签背景色（浅色） */
export const getTagColor = (index: number) => {
  const baseHSL = hexToHSL(theme.value);
  const hueShift = (baseHSL.h + index * 30) % 360; // 每个标签色相偏移30度
  return HSLToHex(
    hueShift,
    Math.max(0, baseHSL.s - 15), // 降低饱和度
    Math.min(95, baseHSL.l + 45) // 提高亮度
  );
}

/* 生成边框颜色（比背景深） */
export const getBorderColor = (index: number) => {
  const baseHSL = hexToHSL(theme.value);
  const hueShift = (baseHSL.h + index * 30) % 360;
  return HSLToHex(
    hueShift,
    baseHSL.s, // 保持原饱和度
    Math.max(20, baseHSL.l - 10) // 降低亮度
  );
}

/* 生成字体颜色（最深） */
export const getFontColor = (index: number) => {
  const baseHSL = hexToHSL(theme.value);
  const hueShift = (baseHSL.h + index * 30) % 360;
  return HSLToHex(
    hueShift,
    Math.min(100, baseHSL.s + 15), // 增加饱和度
    Math.max(10, baseHSL.l - 30) // 显著降低亮度
  );
}
// 颜色转换工具函数
export const hexToHsl = (hex: string) => {
  // 移除可能的#号
  const hexSanitized = hex.replace('#', '')

  // 解析RGB值
  const r = parseInt(hexSanitized.substring(0,2), 16)/255
  const g = parseInt(hexSanitized.substring(2,4), 16)/255
  const b = parseInt(hexSanitized.substring(4,6), 16)/255

  const max = Math.max(r,g,b)
  const min = Math.min(r,g,b)

  let h = 0, s = 0, l = (max + min)/2

  if(max !== min) {
    const d = max - min
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min)

    switch(max){
      case r: h = (g - b)/d + (g < b ? 6 : 0); break
      case g: h = (b - r)/d + 2; break
      case b: h = (r - g)/d + 4; break
    }
    h = Math.round(h * 60)
  }

  return {
    h: Math.round(h),
    s: Math.round(s * 100),
    l: Math.round(l * 100)
  }
}

export const hslToHex = (hsl: { h: number, s: number, l: number }) => {
  const h = hsl.h / 360
  const s = hsl.s / 100
  const l = hsl.l / 100

  let r, g, b

  if(s === 0) {
    r = g = b = l // 灰色系
  } else {
    const hue2rgb = (p: number, q: number, t: number) => {
      if(t < 0) t += 1
      if(t > 1) t -= 1
      if(t < 1/6) return p + (q - p) * 6 * t
      if(t < 1/2) return q
      if(t < 2/3) return p + (q - p) * (2/3 - t) * 6
      return p
    }

    const q = l < 0.5 ? l * (1 + s) : l + s - l * s
    const p = 2 * l - q

    r = hue2rgb(p, q, h + 1/3)
    g = hue2rgb(p, q, h)
    b = hue2rgb(p, q, h - 1/3)
  }

  const toHex = (x: number) => {
    const hex = Math.round(x * 255).toString(16)
    return hex.length === 1 ? '0' + hex : hex
  }

  return `#${toHex(r)}${toHex(g)}${toHex(b)}`
}

/*对题目选项渲染*/
export const getChooseLabel=(index:number|string)=>{
  let list=['A ','B ','C ','D ','E ','F ','G ','H ','I ','J ','K ','L ','M ','N ','O ','P ','Q ','R ','S ','T ','U ','V ','W ','X ','Y ','Z ']
  // @ts-ignore
  return list[index]
}
